package com.shankungfu;

import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.os.StrictMode;
import android.util.DisplayMetrics;

import com.baidu.frontia.FrontiaApplication;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.shankungfu.bean.UserBean;
import com.shankungfu.util.PhoneUtils;
import com.tendcloud.tenddata.TCAgent;

/**
 * 程序启动后，初始化数据
 * 
 * @author WangQing 2013-8-9
 */
public class MainApplication extends Application {

	/** 获取屏幕的宽和高 */
	public static int screenHight = 0;
	public static int screenWidth = 0;
	/** 设备的 IMEI */
	public static String IMEI = "";
	/** 定位获取到的城市 */
	public static String city = "北京";
	/** 获取全局的上下文 */
	public static Context context;
	/** 获取用户数据 */
	public static UserBean userBean;

	/** 分享的文本内容,使用完后会置空, 通用的分享内容，可写，可不写 */
	public static String ShareContent = "";
	/** 视频的种类Id ，视频分享专用，可能没有 */
	public static String ShareVideoCategory = "";
	/** 视频的标题 ，视频分享专用 */
	public static String ShareVideoTitle = "";
	/** 视频的url ，视频分享专用 */
	public static String ShareVideoUrl = "";
	/** 设置分享的图片的地址 */
	public static String sharePicPath = "";

	public static boolean userIsLogin = false;
	public static DisplayImageOptions options;

	@Override
	public void onCreate() {
		super.onCreate();
		init();
	}

	/**
	 * 初始化一些数据 WangQing 2013-8-12 void
	 */
	private void init() {
		// 初始化百度 推送
		FrontiaApplication.initFrontiaApplication(getApplicationContext());

		// 初始化 universalimageloader 的图片缓存处理。
		context = getApplicationContext();
		getDeviceWidthAndHeight();
		initImageLoader(getApplicationContext());
		
		TCAgent.init(this);
		TCAgent.setReportUncaughtExceptions(true);
	}

	private void initImageLoader(Context applicationContext) {
		// This configuration tuning is custom. You can tune every option, you
		// may tune some of them,
		// or you can create default configuration by
		// ImageLoaderConfiguration.createDefault(this);
		// method.
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				context).threadPriority(Thread.NORM_PRIORITY - 2)
				.denyCacheImageMultipleSizesInMemory()
				.discCacheFileNameGenerator(new Md5FileNameGenerator())
				.tasksProcessingOrder(QueueProcessingType.LIFO)
				.writeDebugLogs() // Remove for release app
				.build();
		// Initialize ImageLoader with configuration.
		ImageLoader.getInstance().init(config);
		
		options = new DisplayImageOptions.Builder()
		.showImageOnLoading(R.drawable.default_icon)
		.showImageForEmptyUri(R.drawable.default_icon)
		.showImageOnFail(R.drawable.default_icon)
		.imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
		.cacheInMemory(true)
		.cacheOnDisc(true)
		.considerExifParams(false)
		.displayer(new RoundedBitmapDisplayer(10))
		.build();
	}
	
	public static DisplayImageOptions getOptions() {
		return options;
	}

	/**
	 * 获取设备的宽和高 WangQing 2013-8-12 void
	 */
	private void getDeviceWidthAndHeight() {
		DisplayMetrics dm = PhoneUtils.getAppWidthAndHeight(this);
		screenHight = dm.heightPixels;
		screenWidth = dm.widthPixels;
	}

	public static boolean isUserIsLogin() {
		return userIsLogin;
	}

	public static void setUserIsLogin(boolean userIsLogin) {
		MainApplication.userIsLogin = userIsLogin;
	}

	public static String getCity() {
		if (city.contains("省")) {
			city = city.substring(0, city.indexOf("省"));
		} else if (city.contains("市")) {
			city = city.substring(0, city.indexOf("市"));
		}
		
		return city;
	}

	public static void setCity(String city) {
		MainApplication.city = city;
	}

}
